﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200029C RID: 668
	internal class DoOnSubscribeObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000EBD RID: 3773 RVA: 0x00045318 File Offset: 0x00043718
		public DoOnSubscribeObservable(IObservable<T> source, Action onSubscribe) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.onSubscribe = onSubscribe;
		}

		// Token: 0x06000EBE RID: 3774 RVA: 0x00045334 File Offset: 0x00043734
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new DoOnSubscribeObservable<T>.DoOnSubscribe(this, observer, cancel).Run();
		}

		// Token: 0x0400088C RID: 2188
		private readonly IObservable<T> source;

		// Token: 0x0400088D RID: 2189
		private readonly Action onSubscribe;

		// Token: 0x0200029D RID: 669
		private class DoOnSubscribe : OperatorObserverBase<T, T>
		{
			// Token: 0x06000EBF RID: 3775 RVA: 0x00045343 File Offset: 0x00043743
			public DoOnSubscribe(DoOnSubscribeObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000EC0 RID: 3776 RVA: 0x00045354 File Offset: 0x00043754
			public IDisposable Run()
			{
				try
				{
					this.parent.onSubscribe();
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return Disposable.Empty;
				}
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06000EC1 RID: 3777 RVA: 0x000453CC File Offset: 0x000437CC
			public override void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000EC2 RID: 3778 RVA: 0x000453DC File Offset: 0x000437DC
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000EC3 RID: 3779 RVA: 0x00045414 File Offset: 0x00043814
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400088E RID: 2190
			private readonly DoOnSubscribeObservable<T> parent;
		}
	}
}
